<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css"/>




  <link href="//fonts.googleapis.com/css?family=Lato:300,400,700,400italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">



<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=0.5.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="关系型数据库,MySQL," />





  <link rel="alternate" href="/atom.xml" title="Joshua Shaw之秘密花园" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=0.5.0" />






<meta name="description" content="0x00前言本书讲述到定稿前的MySQL5.5版，所以下面内容的适用范围止步于MySQL5.5。本文仅仅强调书中讲述的重中之重，以便快速查阅，详细的内容还请认真阅读书本和MySQL的官方文档。
0x01简介本章阐述所有与复制相关的内容

简要介绍复制如何工作
讨论基本的复制服务搭建
与复制相关的配置
如何管理和优化复制服务器




0x02复制概述
MySQL支持两种复制方式：基于行的复制和基于">
<meta property="og:type" content="article">
<meta property="og:title" content="《高性能MySQL》の复制">
<meta property="og:url" content="http://joshshaw.github.io/2016/04/26/《高性能MySQL》の复制/index.html">
<meta property="og:site_name" content="Joshua Shaw之秘密花园">
<meta property="og:description" content="0x00前言本书讲述到定稿前的MySQL5.5版，所以下面内容的适用范围止步于MySQL5.5。本文仅仅强调书中讲述的重中之重，以便快速查阅，详细的内容还请认真阅读书本和MySQL的官方文档。
0x01简介本章阐述所有与复制相关的内容

简要介绍复制如何工作
讨论基本的复制服务搭建
与复制相关的配置
如何管理和优化复制服务器




0x02复制概述
MySQL支持两种复制方式：基于行的复制和基于">
<meta property="og:updated_time" content="2016-04-28T11:21:59.653Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="《高性能MySQL》の复制">
<meta name="twitter:description" content="0x00前言本书讲述到定稿前的MySQL5.5版，所以下面内容的适用范围止步于MySQL5.5。本文仅仅强调书中讲述的重中之重，以便快速查阅，详细的内容还请认真阅读书本和MySQL的官方文档。
0x01简介本章阐述所有与复制相关的内容

简要介绍复制如何工作
讨论基本的复制服务搭建
与复制相关的配置
如何管理和优化复制服务器




0x02复制概述
MySQL支持两种复制方式：基于行的复制和基于">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"always"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>

  <title> 《高性能MySQL》の复制 | Joshua Shaw之秘密花园 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?99d95abecc387f2aa83d123804dddd4c";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Joshua Shaw之秘密花园</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">成就自己</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-home fa-fw"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-user fa-fw"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-th fa-fw"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-archive fa-fw"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-tags fa-fw"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                《高性能MySQL》の复制
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-04-26T19:19:20+08:00" content="2016-04-26">
              2016-04-26
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/计算机科学/" itemprop="url" rel="index">
                    <span itemprop="name">计算机科学</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/04/26/《高性能MySQL》の复制/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/04/26/《高性能MySQL》の复制/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="0x00前言"><a href="#0x00前言" class="headerlink" title="0x00前言"></a>0x00前言</h2><p>本书讲述到定稿前的MySQL5.5版，所以下面内容的适用范围止步于MySQL5.5。本文仅仅强调书中讲述的重中之重，<br>以便快速查阅，详细的内容还请认真阅读书本和MySQL的官方文档。</p>
<h2 id="0x01简介"><a href="#0x01简介" class="headerlink" title="0x01简介"></a>0x01简介</h2><p>本章阐述所有与复制相关的内容</p>
<ol>
<li>简要介绍复制如何工作</li>
<li>讨论基本的复制服务搭建<ul>
<li>与复制相关的配置</li>
<li>如何管理和优化复制服务器</li>
</ul>
</li>
</ol>
<hr>
<h2 id="0x02复制概述"><a href="#0x02复制概述" class="headerlink" title="0x02复制概述"></a>0x02复制概述</h2><ul>
<li>MySQL支持两种复制方式：基于行的复制和基于语句的复制。</li>
<li>都是通过主库上记录二进制日志，虽然有开销，但是不会很大。</li>
<li>同一时间点备库上的数据可能与主库存在不一致性，并无法保证主备之间的延迟。</li>
<li>通过复制可以将读操作指向备库来获得更好地读扩展。</li>
<li>目前备库只能串行化执行。</li>
</ul>
<h3 id="复制解决的问题"><a href="#复制解决的问题" class="headerlink" title="复制解决的问题"></a>复制解决的问题</h3><ul>
<li>数据分布</li>
<li>负载均衡</li>
<li>备份</li>
<li>高可用性和故障切换</li>
<li>MySQL升级</li>
</ul>
<h3 id="复制如何工作"><a href="#复制如何工作" class="headerlink" title="复制如何工作"></a>复制如何工作</h3><ul>
<li>主库把数据更改记录到二进制日志中。</li>
<li>备库将主库上的日志复制到自己的中继日志中。</li>
<li>备库读取中继日志中的事件，将其重放到备库数据之上。</li>
</ul>
<a id="more"></a>
<h2 id="0x03配置复制"><a href="#0x03配置复制" class="headerlink" title="0x03配置复制"></a>0x03配置复制</h2><p>（略）</p>
<hr>
<h2 id="0x04复制的原理"><a href="#0x04复制的原理" class="headerlink" title="0x04复制的原理"></a>0x04复制的原理</h2><p>现在一般使用基于行的复制更佳。</p>
<h3 id="基于语句的复制（逻辑复制）"><a href="#基于语句的复制（逻辑复制）" class="headerlink" title="基于语句的复制（逻辑复制）"></a>基于语句的复制（逻辑复制）</h3><h4 id="优点"><a href="#优点" class="headerlink" title="优点"></a>优点</h4><ul>
<li>实现相当简单</li>
<li>不用太多带宽</li>
<li>容易理解</li>
</ul>
<h4 id="缺点"><a href="#缺点" class="headerlink" title="缺点"></a>缺点</h4><ul>
<li>很多情况无法正确复制，如使用了now()等函数</li>
<li>若使用了触发器或者存储过程也最好不要使用</li>
</ul>
<h3 id="基于行的复制"><a href="#基于行的复制" class="headerlink" title="基于行的复制"></a>基于行的复制</h3><h4 id="优点-1"><a href="#优点-1" class="headerlink" title="优点"></a>优点</h4><ul>
<li>减少锁的使用，更高效地复制数据</li>
<li>占用更少的CPU</li>
<li>解决数据不一致的情况</li>
</ul>
<h4 id="缺点-1"><a href="#缺点-1" class="headerlink" title="缺点"></a>缺点</h4><ul>
<li>无法知道服务器正在做什么</li>
<li>无法处理诸如在备库修改表的schema的情况</li>
<li>带宽较高</li>
</ul>
<hr>
<h2 id="0x05复制拓扑（往后补充图）"><a href="#0x05复制拓扑（往后补充图）" class="headerlink" title="0x05复制拓扑（往后补充图）"></a>0x05复制拓扑（往后补充图）</h2><p>MySQL的复制有一个限制：每个备库只能有一个主库，但是可以用一些其他方法来解决这样的限制。</p>
<h3 id="一主库多备库（多用于备份和读写分离）"><a href="#一主库多备库（多用于备份和读写分离）" class="headerlink" title="一主库多备库（多用于备份和读写分离）"></a>一主库多备库（多用于备份和读写分离）</h3><p>备库之间根本没有交互。有以下用途：</p>
<ul>
<li>为不同的角色使用不同的备库</li>
<li>可把一个备库当做代用的主库</li>
<li>把备库放在远程数据中心，用作灾难恢复</li>
<li>备份，培训，开发，测试</li>
</ul>
<h3 id="主动-主动模式下的主-主复制"><a href="#主动-主动模式下的主-主复制" class="headerlink" title="主动-主动模式下的主-主复制"></a>主动-主动模式下的主-主复制</h3><p>用于两个处于不同地理位置的办公室，并且都需要一份可写的数据拷贝。弊大于利。</p>
<h3 id="主动-被动模式下的主-主复制"><a href="#主动-被动模式下的主-主复制" class="headerlink" title="主动-被动模式下的主-主复制"></a>主动-被动模式下的主-主复制</h3><p>切换主动被动服务器很方便。</p>
<h3 id="拥有备库的主-主结构"><a href="#拥有备库的主-主结构" class="headerlink" title="拥有备库的主-主结构"></a>拥有备库的主-主结构</h3><p>（用于增加冗余）</p>
<h3 id="环形复制"><a href="#环形复制" class="headerlink" title="环形复制"></a>环形复制</h3><p>（要避免成环）</p>
<h3 id="主库、分发主库以及备库"><a href="#主库、分发主库以及备库" class="headerlink" title="主库、分发主库以及备库"></a>主库、分发主库以及备库</h3><ul>
<li>当备库足够多时。会对主库造成很大的负载，于是需要采用blackhole存储引擎的分发主库。</li>
<li>不一定只使用一个分发主库。</li>
<li>blackhole表没有任何数据，但是目前有bug</li>
</ul>
<h3 id="树或金字塔形"><a href="#树或金字塔形" class="headerlink" title="树或金字塔形"></a>树或金字塔形</h3><p>(多台备库)</p>
<h3 id="定制的复制方案"><a href="#定制的复制方案" class="headerlink" title="定制的复制方案"></a>定制的复制方案</h3><ul>
<li>选择复制性</li>
<li>分离功能（分离OLTP和OLAP）</li>
<li>数据归档</li>
<li>将备库用作全文索引</li>
<li>只读备库</li>
<li>模拟多主库复制</li>
<li>创建没有数据的日志服务器</li>
</ul>
<hr>
<h2 id="0x06复制和容量规划"><a href="#0x06复制和容量规划" class="headerlink" title="0x06复制和容量规划"></a>0x06复制和容量规划</h2><p>主备库的模式下，并不是增加备库就能线性增加读写功能。并且在开启复制功能时，要考虑监控延时，可用性（各个<br>库的状态，包括主和被），规划冗余容量和性能，改变主库，还有解决问题的方法。</p>
<hr>
<h2 id="0x07-MySQL复制的高级特性"><a href="#0x07-MySQL复制的高级特性" class="headerlink" title="0x07 MySQL复制的高级特性"></a>0x07 MySQL复制的高级特性</h2><h3 id="半同步复制"><a href="#半同步复制" class="headerlink" title="半同步复制"></a>半同步复制</h3><p>可以帮助确保备库拥有主库数据的拷贝，减少潜在的数据丢失危机。有助于备库提供更好地冗余度和持久性。<br>半同步复制在提交过程中增加一个延迟：当提交事务时，在客户端接收到查询结束反馈前必须保证二进制日志已经传输<br>到至少一台备库上。</p>
<ul>
<li>在主库上已经完成<strong>事务</strong>提交，只有通知客户端被延迟了。</li>
<li>备库在接<strong>收到事务后</strong>发送反馈而非（备库）完成事务后发送。</li>
<li>如果备库一直没有回应已收到事件，会超时并转化为正常的异步复制模式。</li>
</ul>
<h3 id="复制心跳"><a href="#复制心跳" class="headerlink" title="复制心跳"></a>复制心跳</h3><p>保持备库一直与主库相联系，避免悄无声息地断开连接。</p>
<hr>
<h2 id="0x08小结"><a href="#0x08小结" class="headerlink" title="0x08小结"></a>0x08小结</h2><ul>
<li>KISS原则（Keep It Simple Stupid），用简单的就好。</li>
<li>监控，监控，监控，重要的事情说三遍。</li>
<li>理解复制的异步本质，且设计你的应用避免或容忍从备库读取脏数据。</li>
<li>在一个复制拓扑中不要写入多于一个服务器，把备库配置为只读，并降低权限以阻止对数据的改变。</li>
<li>打开本章锁讨论的明智且安全的设置。（往后补充）</li>
</ul>
<hr>
<h2 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h2><p><a href="https://book.douban.com/subject/4241826/" target="_blank" rel="external">《高性能MySQL · 第三版》</a></p>

      
    </div>

    <div>
      
        
      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/关系型数据库/" rel="tag">#关系型数据库</a>
          
            <a href="/tags/MySQL/" rel="tag">#MySQL</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/04/22/《高性能MySQL》のMySQL高级特性/" rel="next" title="《高性能MySQL》のMySQL高级特性">
                <i class="fa fa-chevron-left"></i> 《高性能MySQL》のMySQL高级特性
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/04/28/《高性能MySQL》のSchema与数据类型优化/" rel="prev" title="《高性能MySQL》のSchema与数据类型优化">
                《高性能MySQL》のSchema与数据类型优化 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
  <a class="jiathis_button_tsina"></a>
  <a class="jiathis_button_tqq"></a>
  <a class="jiathis_button_weixin"></a>
  <a class="jiathis_button_cqq"></a>
  <a class="jiathis_button_douban"></a>
  <a class="jiathis_button_renren"></a>
  <a class="jiathis_button_qzone"></a>
  <a class="jiathis_button_kaixin001"></a>
  <a class="jiathis_button_copy"></a>
  <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank"></a>
  <a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
  var jiathis_config={
    hideMore:false
  }
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END -->

      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/04/26/《高性能MySQL》の复制/"
           data-title="《高性能MySQL》の复制" data-url="http://joshshaw.github.io/2016/04/26/《高性能MySQL》の复制/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="Joshua Shaw" />
          <p class="site-author-name" itemprop="name">Joshua Shaw</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">20</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">26</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/joshuashawnetlab" target="_blank">
                  
                    <i class="fa fa-globe"></i> Github_1
                  
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://github.com/joshshaw" target="_blank">
                  
                    <i class="fa fa-globe"></i> Github_2
                  
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/u/2622463195" target="_blank">
                  
                    <i class="fa fa-weibo"></i> Weibo
                  
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://my.oschina.net/joshuashaw" target="_blank">
                  
                    <i class="fa fa-globe"></i> OSChina
                  
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.zhihu.com/people/xiao-chong-chong-41" target="_blank">
                  
                    <i class="fa fa-globe"></i> Zhihu
                  
                </a>
              </span>
            
          
        </div>

        
        

        
        <div class="links-of-blogroll motion-element">
          
        </div>

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc-indicator-top post-toc-indicator">
            <i class="fa fa-angle-double-up"></i>
          </div>
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#0x00前言"><span class="nav-number">1.</span> <span class="nav-text">0x00前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#0x01简介"><span class="nav-number">2.</span> <span class="nav-text">0x01简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#0x02复制概述"><span class="nav-number">3.</span> <span class="nav-text">0x02复制概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#复制解决的问题"><span class="nav-number">3.1.</span> <span class="nav-text">复制解决的问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#复制如何工作"><span class="nav-number">3.2.</span> <span class="nav-text">复制如何工作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#0x03配置复制"><span class="nav-number">4.</span> <span class="nav-text">0x03配置复制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#0x04复制的原理"><span class="nav-number">5.</span> <span class="nav-text">0x04复制的原理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#基于语句的复制（逻辑复制）"><span class="nav-number">5.1.</span> <span class="nav-text">基于语句的复制（逻辑复制）</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#优点"><span class="nav-number">5.1.1.</span> <span class="nav-text">优点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#缺点"><span class="nav-number">5.1.2.</span> <span class="nav-text">缺点</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基于行的复制"><span class="nav-number">5.2.</span> <span class="nav-text">基于行的复制</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#优点-1"><span class="nav-number">5.2.1.</span> <span class="nav-text">优点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#缺点-1"><span class="nav-number">5.2.2.</span> <span class="nav-text">缺点</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#0x05复制拓扑（往后补充图）"><span class="nav-number">6.</span> <span class="nav-text">0x05复制拓扑（往后补充图）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#一主库多备库（多用于备份和读写分离）"><span class="nav-number">6.1.</span> <span class="nav-text">一主库多备库（多用于备份和读写分离）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#主动-主动模式下的主-主复制"><span class="nav-number">6.2.</span> <span class="nav-text">主动-主动模式下的主-主复制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#主动-被动模式下的主-主复制"><span class="nav-number">6.3.</span> <span class="nav-text">主动-被动模式下的主-主复制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#拥有备库的主-主结构"><span class="nav-number">6.4.</span> <span class="nav-text">拥有备库的主-主结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#环形复制"><span class="nav-number">6.5.</span> <span class="nav-text">环形复制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#主库、分发主库以及备库"><span class="nav-number">6.6.</span> <span class="nav-text">主库、分发主库以及备库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#树或金字塔形"><span class="nav-number">6.7.</span> <span class="nav-text">树或金字塔形</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#定制的复制方案"><span class="nav-number">6.8.</span> <span class="nav-text">定制的复制方案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#0x06复制和容量规划"><span class="nav-number">7.</span> <span class="nav-text">0x06复制和容量规划</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#0x07-MySQL复制的高级特性"><span class="nav-number">8.</span> <span class="nav-text">0x07 MySQL复制的高级特性</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#半同步复制"><span class="nav-number">8.1.</span> <span class="nav-text">半同步复制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#复制心跳"><span class="nav-number">8.2.</span> <span class="nav-text">复制心跳</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#0x08小结"><span class="nav-number">9.</span> <span class="nav-text">0x08小结</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#引用"><span class="nav-number">10.</span> <span class="nav-text">引用</span></a></li></ol></div>
            
          </div>
          <div class="post-toc-indicator-bottom post-toc-indicator">
            <i class="fa fa-angle-double-down"></i>
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Joshua Shaw</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>



      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  


  




<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  <script type="text/javascript" src="/vendors/velocity/velocity.min.js"></script>

  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js"></script>

  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=0.5.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=0.5.0"></script>



  
  

  
  
<script type="text/javascript" src="/js/src/scrollspy.js?v=0.5.0"></script>

<script type="text/javascript" id="sidebar.toc.highlight">
  $(document).ready(function () {
    var tocSelector = '.post-toc';
    var $tocSelector = $(tocSelector);
    var activeCurrentSelector = '.active-current';

    $tocSelector
      .on('activate.bs.scrollspy', function () {
        var $currentActiveElement = $(tocSelector + ' .active').last();

        removeCurrentActiveClass();
        $currentActiveElement.addClass('active-current');

        $tocSelector[0].scrollTop = $currentActiveElement.position().top;
      })
      .on('clear.bs.scrollspy', function () {
        removeCurrentActiveClass();
      });

    function removeCurrentActiveClass () {
      $(tocSelector + ' ' + activeCurrentSelector)
        .removeClass(activeCurrentSelector.substring(1));
    }

    function processTOC () {
      getTOCMaxHeight();
      toggleTOCOverflowIndicators();
    }

    function getTOCMaxHeight () {
      var height = $('.sidebar').height() -
                   $tocSelector.position().top -
                   $('.post-toc-indicator-bottom').height();

      $tocSelector.css('height', height);

      return height;
    }

    function toggleTOCOverflowIndicators () {
      tocOverflowIndicator(
        '.post-toc-indicator-top',
        $tocSelector.scrollTop() > 0 ? 'show' : 'hide'
      );

      tocOverflowIndicator(
        '.post-toc-indicator-bottom',
        $tocSelector.scrollTop() >= $tocSelector.find('ol').height() - $tocSelector.height() ? 'hide' : 'show'
      )
    }

    $(document).on('sidebar.motion.complete', function () {
      processTOC();
    });

    $('body').scrollspy({ target: tocSelector });
    $(window).on('resize', function () {
      if ( $('.sidebar').hasClass('sidebar-active') ) {
        processTOC();
      }
    });

    onScroll($tocSelector);

    function onScroll (element) {
      element.on('mousewheel DOMMouseScroll', function (event) {
          var oe = event.originalEvent;
          var delta = oe.wheelDelta || -oe.detail;

          this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
          event.preventDefault();

          toggleTOCOverflowIndicators();
      });
    }

    function tocOverflowIndicator (indicator, action) {
      var $indicator = $(indicator);
      var opacity = action === 'show' ? 1 : 0;
      $indicator.velocity ?
        $indicator.velocity('stop').velocity({
          opacity: opacity
        }, { duration: 100 }) :
        $indicator.stop().animate({
          opacity: opacity
        }, 100);
    }

  });
</script>

<script type="text/javascript" id="sidebar.nav">
  $(document).ready(function () {
    var html = $('html');
    var TAB_ANIMATE_DURATION = 200;
    var hasVelocity = $.isFunction(html.velocity);

    $('.sidebar-nav li').on('click', function () {
      var item = $(this);
      var activeTabClassName = 'sidebar-nav-active';
      var activePanelClassName = 'sidebar-panel-active';
      if (item.hasClass(activeTabClassName)) {
        return;
      }

      var currentTarget = $('.' + activePanelClassName);
      var target = $('.' + item.data('target'));

      hasVelocity ?
        currentTarget.velocity('transition.slideUpOut', TAB_ANIMATE_DURATION, function () {
          target
            .velocity('stop')
            .velocity('transition.slideDownIn', TAB_ANIMATE_DURATION)
            .addClass(activePanelClassName);
        }) :
        currentTarget.animate({ opacity: 0 }, TAB_ANIMATE_DURATION, function () {
          currentTarget.hide();
          target
            .stop()
            .css({'opacity': 0, 'display': 'block'})
            .animate({ opacity: 1 }, TAB_ANIMATE_DURATION, function () {
              currentTarget.removeClass(activePanelClassName);
              target.addClass(activePanelClassName);
            });
        });

      item.siblings().removeClass(activeTabClassName);
      item.addClass(activeTabClassName);
    });

    $('.post-toc a').on('click', function (e) {
      e.preventDefault();
      var targetSelector = NexT.utils.escapeSelector(this.getAttribute('href'));
      var offset = $(targetSelector).offset().top;
      hasVelocity ?
        html.velocity('stop').velocity('scroll', {
          offset: offset  + 'px',
          mobileHA: false
        }) :
        $('html, body').stop().animate({
          scrollTop: offset
        }, 500);
    });

    // Expand sidebar on post detail page by default, when post has a toc.
    NexT.motion.middleWares.sidebar = function () {
      var $tocContent = $('.post-toc-content');

      if (CONFIG.sidebar.display === 'post' || CONFIG.sidebar.display === 'always') {
        if ($tocContent.length > 0 && $tocContent.html().trim().length > 0) {
          NexT.utils.displaySidebar();
        }
      }
    };
  });
</script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=0.5.0"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"joshuashaw"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
  





  
  
  

  


</body>
</html>
